﻿Imports Publics

Public Class FrmDieuKienLoc_BaoCaoNXTVatTu
    Private FrmChonKho As Frm_DieuKienLoc_ChonKho
    Private FrmChonVT As Frm_DieuKienLoc_ChonVatTu

    Private lstSourceAll As New List(Of clsNhapXuatTonBaseVatTu)
    Private lstDauKy As New List(Of clsNhapXuatTonBaseVatTu)
    Private lstPSNhap As New List(Of clsPhatSinhVatTu)
    Private lstPSXuat As New List(Of clsPhatSinhVatTu)

    Private lstDMVatTu As List(Of clsDMChon)
    Private lstDMKho As List(Of clsDMChon)
    Private lstIDKho As List(Of String)
    Private lstIDVatTu As List(Of String)

    Private _TuNgay As Date
    Public Property TuNgay() As Date
        Get
            Return _TuNgay
        End Get
        Set(ByVal value As Date)
            _TuNgay = value
        End Set
    End Property

    Private _DenNgay As Date
    Public Property DenNgay() As Date
        Get
            Return _DenNgay
        End Get
        Set(ByVal value As Date)
            _DenNgay = value
        End Set
    End Property

    Private Sub btnThoat_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnThoat.ItemClick
        Me.Close()
    End Sub

    Private Sub btnLapBaoCao_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles btnLapBaoCao.ItemClick
        GetAllData()
        XuLyData()
        Dim frm As New FrmBaoCaoNhapXuatTonVatTu
        frm.TuNgay = TuNgay
        frm.DenNgay = DenNgay

        frm.lstSource_All = lstSourceAll
        frm.ShowDialog()

    End Sub
    Private Sub GetAllData()
        lstSourceAll.Clear()
        lstDauKy.Clear()
        lstPSNhap.Clear()
        lstPSXuat.Clear()
        Dim intNamDK = TuNgay.Year
        lstDauKy = (From ds In dbTV.VT_SoDuDKs Where ds.InUsed AndAlso ds.Nam = intNamDK Select New clsNhapXuatTonBaseVatTu With { _
                    .IDKho = ds.IDKho, .IDVatTu = ds.IDVatTu, .SLDK = ds.SoLuong.GetValueOrDefault}).ToList

        lstPSNhap = (From ds In dbTV.VT_HDNhap_ChiTiets Join h In dbTV.VT_HDNhaps On ds.IDHDNhap Equals h.ID _
                     Where ds.InUsed AndAlso h.InUsed AndAlso h.NgayCT.Value.Date >= TuNgay AndAlso h.NgayCT.Value.Date <= DenNgay _
                     Select New clsPhatSinhVatTu With {.NgayCT = h.NgayCT.GetValueOrDefault.Date, _
                     .IDKho = ds.IDKho, .IDVatTu = ds.IDVatTu, .SoLuong = ds.SoLuong.GetValueOrDefault}).ToList

        lstPSXuat = (From ds In dbTV.VT_HDXuat_ChiTiets Join h In dbTV.VT_HDXuats On ds.IDHDXuat Equals h.ID _
                     Where ds.InUsed AndAlso h.InUsed AndAlso h.NgayCT.Value >= TuNgay AndAlso h.NgayCT.Value <= DenNgay _
                     Select New clsPhatSinhVatTu With {.NgayCT = h.NgayCT.GetValueOrDefault.Date, _
                     .IDKho = ds.IDKho, .IDVatTu = ds.IDVatTu, .SoLuong = ds.SoLuong.GetValueOrDefault}).ToList


    End Sub

    Private Sub FrmDieuKienLoc_BaoCaoNXTVatTu_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Windows.Forms.Keys.Escape Then
            btnThoat_ItemClick(Nothing, Nothing)
        End If
    End Sub

    Private Sub FrmDieuKienLoc_BaoCaoNXTVatTu_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        lstDMKho = (From ds In dbTV.DM_Khos Where ds.InUsed Select New clsDMChon With {.ID = ds.ID, .Ma = ds.MaKho, .Ten = ds.TenKho}).ToList
        lstIDKho = (From ds In lstDMKho Select ds.ID).ToList
        lstDMVatTu = (From ds In dbTV.VT_DM_VatTus Where ds.InUsed Select New clsDMChon With {.ID = ds.ID, .Ma = ds.MaVatTu, .Ten = ds.TenVatTu, .DonGia = ds.GiaBan, .DVT = ds.DVT}).ToList
        lstIDVatTu = (From ds In lstDMVatTu Select ds.ID).ToList

    End Sub

    Private Sub XuLyData()
        Dim lstSource As New List(Of clsNhapXuatTonBaseVatTu)
        Dim DauNam As Date = DateSerial(TuNgay.Year, 1, 1)

        Dim lstTMP_NK_TMP = (From ds In lstPSNhap Where ds.NgayCT >= DauNam AndAlso ds.NgayCT < TuNgay _
                             Select New clsNhapXuatTonBaseVatTu With { _
                             .IDVatTu = ds.IDVatTu, .IDKho = ds.IDKho, .SLNK_TMP = ds.SoLuong}).ToList
        Dim lstTMP_XK_TMP = (From ds In lstPSXuat Where ds.NgayCT >= DauNam AndAlso ds.NgayCT < TuNgay _
                             Select New clsNhapXuatTonBaseVatTu With { _
                             .IDVatTu = ds.IDVatTu, .IDKho = ds.IDKho, .SLNK_TMP = ds.SoLuong}).ToList

        Dim lstTMP_NK = (From ds In lstPSNhap Where ds.NgayCT >= TuNgay AndAlso ds.NgayCT <= DenNgay _
                          Select New clsNhapXuatTonBaseVatTu With { _
                          .IDKho = ds.IDKho, .IDVatTu = ds.IDVatTu, .SLNK = ds.SoLuong}).ToList

        Dim lstTMP_XK = (From ds In lstPSXuat Where ds.NgayCT >= TuNgay AndAlso ds.NgayCT <= DenNgay _
                         Select New clsNhapXuatTonBaseVatTu With { _
                         .IDKho = ds.IDKho, .IDVatTu = ds.IDVatTu, .SLXK = ds.SoLuong}).ToList

        lstSource.AddRange(lstDauKy)
        lstSource.AddRange(lstTMP_NK_TMP)
        lstSource.AddRange(lstTMP_NK)
        lstSource.AddRange(lstTMP_XK_TMP)
        lstSource.AddRange(lstTMP_XK)

        lstSourceAll = (From ds In lstSource Group By ds.IDKho, ds.IDVatTu Into g = Group _
                        Select New clsNhapXuatTonBaseVatTu With { _
                        .IDKho = IDKho, .IDVatTu = IDVatTu, _
                        .SLDK = g.Sum(Function(c As clsNhapXuatTonBaseVatTu) c.SLDK) - g.Sum(Function(c As clsNhapXuatTonBaseVatTu) c.SLNK_TMP), _
                        .SLNK = g.Sum(Function(c As clsNhapXuatTonBaseVatTu) c.SLNK), _
                        .SLXK = g.Sum(Function(c As clsNhapXuatTonBaseVatTu) c.SLXK), _
                        .SLCK = .SLDK + .SLNK - .SLXK}).ToList


        If lstIDVatTu.Count <> lstDMVatTu.Count Then
            lstSourceAll = (From ds In lstSourceAll Join ds1 In lstIDVatTu On ds.IDVatTu Equals ds1 Select ds).ToList
        End If

        If lstIDKho.Count <> lstDMKho.Count Then
            lstSourceAll = (From ds In lstSourceAll Join ds1 In lstIDKho On ds.IDKho Equals ds1 Select ds).ToList
        End If

        For Each item In (From ds In lstSourceAll Join ds1 In lstDMKho On ds.IDKho Equals ds1.ID)
            item.ds.MaKho = item.ds1.Ma
            item.ds.TenKho = item.ds1.Ten
        Next
        For Each item In From ds In lstSourceAll Join ds1 In lstDMVatTu On ds.IDVatTu Equals ds1.ID
            item.ds.MaVT = item.ds1.Ma
            item.ds.TenVT = item.ds1.Ten
            item.ds.DVT = item.ds1.DVT

            item.ds.GVDK = item.ds.SLDK * item.ds1.DonGia
            item.ds.GVNK = item.ds.SLNK * item.ds1.DonGia
            item.ds.GVXK = item.ds.SLXK * item.ds1.DonGia
            item.ds.GVCK = item.ds.SLCK * item.ds1.DonGia

        Next
    End Sub

    Private Sub chkVatTu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkVatTu.Click
        VatTu()
    End Sub

    Private Sub chkKho_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkKho.Click
        Kho()
    End Sub

    Private Sub VatTu()
        If FrmChonVT Is Nothing Then
            FrmChonVT = New Frm_DieuKienLoc_ChonVatTu
            FrmChonVT.lstID = lstIDVatTu
        End If

        FrmChonVT.StartPosition = Windows.Forms.FormStartPosition.CenterScreen
        FrmChonVT.ShowDialog()
        If FrmChonVT.Thoat = False Then lstIDVatTu = FrmChonVT.lstID
        If lstIDVatTu.Count = lstDMVatTu.Count Then
            chkVatTu.Checked = False
            chkVatTu.Text = " 1. Tất cả vật tư"
            chkVatTu.ForeColor = System.Drawing.Color.Black
            chkVatTu.Refresh()
        Else
            chkVatTu.Checked = True
            chkVatTu.Text = " 1. Đã chọn vật tư"
            chkVatTu.ForeColor = System.Drawing.Color.Red
            chkVatTu.Refresh()
        End If
    End Sub

    Private Sub Kho()
        If FrmChonKho Is Nothing Then
            FrmChonKho = New Frm_DieuKienLoc_ChonKho
            FrmChonKho.lstID = lstIDKho
        End If
        FrmChonKho.StartPosition = Windows.Forms.FormStartPosition.CenterScreen
        FrmChonKho.ShowDialog()
        Dim count = lstDMKho.Count
        If FrmChonKho.Thoat = False Then lstIDKho = FrmChonKho.lstID
        If lstIDKho.Count = count Then
            chkKho.Checked = False
            chkKho.Text = " 2. Tất cả kho"
            chkKho.ForeColor = System.Drawing.Color.Black
            chkKho.Refresh()
        Else
            chkKho.Checked = True
            chkKho.Text = " 2. Đã chọn kho"
            chkKho.ForeColor = System.Drawing.Color.Red
            chkKho.Refresh()
        End If
    End Sub

End Class